package com.ewing.busi.carry.dto;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.CollectionUtils;

import com.ewing.busi.carry.helper.WebCarryHelper;
import com.ewing.busi.sysparam.constants.SysParamRootCodes;
import com.ewing.busi.sysparam.dto.SysParamManager;
import com.ewing.utils.MapUtils;
import com.google.common.collect.Lists;

/**
 * 
 * @author chenxuegui.cxg@alibaba-inc.com
 * @createDate 2016年9月4日
 */
public class WebCarryWayResp {

  private String carryWay;
  
  private String carryWayStr;

  private static final DecimalFormat df = new DecimalFormat("#.00");

  private String carryPrice;

  public static List<WebCarryWayResp> to(Map<String, List<WebCarryWayDto>> map,
      Map<Integer, Integer> resId2Count) {
    if (MapUtils.isEmpty(map)) {
      return Collections.emptyList();
    }

    List<WebCarryWayResp> result = Lists.newArrayList();
    for (Entry<String, List<WebCarryWayDto>> entry : map.entrySet()) {
      WebCarryWayResp resp = new WebCarryWayResp();
      result.add(resp);
      resp.carryWay = entry.getKey();
      resp.carryWayStr = SysParamManager.getInstance(SysParamRootCodes.CARRY_WAY).getNameByCode(resp.carryWay);
      if (CollectionUtils.isEmpty(entry.getValue())) {
        resp.carryPrice = df.format(0d);
      }

      double carryPrice = 0d;
      for (WebCarryWayDto dto : entry.getValue()) {
        if (CollectionUtils.isEmpty(dto.getResIdList())) {
          continue;
        }

        int count = 0;
        for (Integer resId : dto.getResIdList()) {
          count += resId2Count.get(resId);
        }
        carryPrice += WebCarryHelper.calcCarryPrice(dto.getFirstAmount(), dto.getFirstFee(),
            dto.getSecAmount(), dto.getSecFee(), count);
      }

      resp.carryPrice = df.format(carryPrice);
    }

    return result;
  }
  
  public WebCarryWayResp(){
  }
  
  public WebCarryWayResp(String carryWay, String carryWayStr,String carryPrice) {
    super();
    this.carryWayStr = carryWayStr;
    this.carryWay = carryWay;
    this.carryPrice = carryPrice;
  }


  public String getCarryWayStr() {
    return carryWayStr;
  }

  public void setCarryWayStr(String carryWayStr) {
    this.carryWayStr = carryWayStr;
  }

  public String getCarryWay() {
    return carryWay;
  }

  public void setCarryWay(String carryWay) {
    this.carryWay = carryWay;
  }

  public String getCarryPrice() {
    return carryPrice;
  }

  public void setCarryPrice(String carryPrice) {
    this.carryPrice = carryPrice;
  }
  
  

}
